<script setup>
import { ref,onMounted } from 'vue'
import useUserInfoStore from '@/stores/userInfo';
import {getAllPatientsService,updatePatientService} from '@/api/admin.js'
const userInfoStore = useUserInfoStore()

const userInfo = ref({...userInfoStore.info})
const patientInfo = ref({})
const rules = {
    nickname: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    gender: [
        { required: true, message: '请选择性别', trigger: 'blur' }
    ],
    idCard:[
        { required: true, message: '请输入身份证号', trigger: 'blur' },
        { pattern: /^[1-9]\d{17}$/, message: '请输入正确的18位身份证号', trigger: 'blur' }
    ],
    name:[
        { required: true, message: '请输入患者姓名', trigger: 'blur' }
    ],
    age:[
        { required: true, message: '请输入患者年龄', trigger: 'blur' },
        { pattern: /^[1-9]\d{1}$/, message: '请输入正确的年龄', trigger: 'blur' }
    ],
    phoneNumber:[
        { required: true, message: '请输入患者电话号码', trigger: 'blur' },
        { pattern: /^[1-9]\d{10}$/, message: '请输入正确的11位电话号码', trigger: 'blur' }
    ]
}
//获取所有患者信息，并通过username匹配patientId
const getAllPatients = async ()=>{
    let result = await getAllPatientsService()
    console.log(result.data);
    result.data.forEach(item=>{
        if(item.username === userInfo.value.username){
            patientInfo.value = item
        }
        
    })
}
//修改用户信息
import {userInfoUpdateService} from '@/api/user.js'
import { ElMessage } from 'element-plus';
const updateUserInfo = async ()=>{
    console.log(userInfo.value);
    console.log(patientInfo.value);
    await userInfoUpdateService(userInfo.value)
    await updatePatientService(patientInfo.value)
    ElMessage.success('修改成功')
    //更新pinia中的数据
    userInfoStore.setInfo(userInfo.value)   
}
onMounted(()=>{
    getAllPatients()
})
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="patientInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="登录名称">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="身份信息">
                        <el-input v-model="userInfo.nickname" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="患者姓名" prop="name">
                        <el-input v-model="patientInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="gender">
                        <el-select v-model="patientInfo.gender" placeholder="请选择性别">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="年龄" prop="age">
                        <el-input v-model.number="patientInfo.age"></el-input>
                    </el-form-item>
                    <el-form-item label="身份证号" prop="idCard">
                        <el-input v-model="patientInfo.idCard"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" prop="phoneNumber">
                        <el-input v-model="patientInfo.phoneNumber"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>